﻿using System.Text.RegularExpressions;
using com.fukefu.source;

namespace com.fukefu.info.baiduzhidao
{
    public class OpenZhidao
    {
        Source source = new Source();

        public bool Login(string name, string pass, string vcode, string host)
        {
            string html = source.Get(host);
            html = source.Get(host + "/user/login_start?Provider=baidu");
            string url = Regex.Match(html, "url=([^\"]+)\"").Groups[1].Value;
            html = source.Get("https://passport.baidu.com/api/?login&tpl=pp&time=" + MyFunction.GetTimestampNow() + "&callback=CBbcmnzy");
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("verifycode", vcode);
            pd.Add("staticpage", "http://openapi.baidu.com/static/common/popup_login/jump.html");
            pd.Add("callback", Regex.Match(html, "&callback=([^\"]+)").Groups[1].Value);
            pd.Add("token", Regex.Match(html, "param1_contex : \"([^\"]+)\"").Groups[1].Value);
            pd.Add("tpl", Regex.Match(html, "param2_contex : \"([^\"]+)\"").Groups[1].Value);
            pd.Add("time", Regex.Match(html, "param3_contex : \"([^\"]+)\"").Groups[1].Value);
            html = source.Post("https://passport.baidu.com/api/?login", pd.Read(), url);
            if (html.IndexOf("error=0") != -1)
            {
                html = source.Get(url);
                html = source.Get(host);
                if (html.IndexOf(" <h4>欢迎你") != -1)
                {
                    return true;
                }
            }
            return false;
        }
        //提问
        public InformationResult Question(string content, string type, string vcode)
        {
            ParametersData pd = new ParametersData();
            InformationResult result = new InformationResult();
            pd.Add("cid", type);
            pd.Add("cid", type);
            pd.Add("cid", type);
            pd.Add("method", "create");
            pd.Add("pic_url", "");
            pd.Add("qcontent", content);
            pd.Add("reward", "0");
            pd.Add("title", content);
            pd.Add("vcode", vcode);
            string html = source.Post("http://zhidao.pcbaby.com.cn/question/", pd.Read());
            if (html.IndexOf("<title>百度知道开放平台_成功提示</title>") != -1)
            {
                result.Code = 1;
                result.Content = "http://zhidao.pcbaby.com.cn/question/" + Regex.Match(html, "'/question/([^']+)';\">查看该问题").Groups[1].Value;
            }
            return result;
        }
        //回答
        public InformationResult Answer(string content, InformationResult result)
        {
            ParametersData pd = new ParametersData();
            pd.Add("time", MyFunction.GetTimestampNow().ToString());
            pd.Add("content", content);
            pd.Add("cite", "");
            pd.Add("pic_url", "");
            pd.Add("qid", result.Content);
            pd.Add("method", "create");
            pd.Add("anonymous", "0");
            string html = source.Post("http://zhidao.pcbaby.com.cn/answer/", pd.Read());
            if (html.IndexOf("您的回答已提交成功") != -1)
            {
                return result;
            }
            return new InformationResult();
        }
    }
}
